
//#define IMAM_COMMS 
#ifdef IMAM_COMMS

#include <http_.h>

#define SERVICE_COMMS_EXCHANGE   200

#ifndef ALARM_WARNING
#define ALARM_BEEP				1
#define ALARM_WARNING			2
#define ALARM_ERROR				3
#endif
//---------------------------------------------
// Comms Device Types
//---------------------------------------------


typedef enum
{
  cmmDeviceNAC,     // 0
  cmmDeviceRS232,     // 1
  cmmDeviceExternal,  // 2
  cmmDeviceEthClient, // 3
  cmmDeviceEthServer,  // 4
  cmmDeviceModem,  //assy
  cmmDevicePPP,
  cmmDeviceUSB,
  cmmDeviceGPRS,
#ifdef _SSL_ENABLE_
  cmmDeviceSSL
#endif
} cmmDevice_t;

//---------------------------------------------
// Connection Status Types
//---------------------------------------------
typedef enum
{
  cmmConnectedNone,
  cmmConnectingPrimary,
  cmmConnectingSecondary,
  cmmConnectedPrimary,
  cmmConnectedSecondary,
  cmmConnectedPeer,
  cmmConnectedGPRS
} cmmConnectionStatus_t;

//---------------------------------------------
// Ethernet Configuration Types
//---------------------------------------------
typedef enum
{
  cmmEthLocal,
  cmmEthPeer,
  cmmEthHostPrimary,
  cmmEthHostSecondary
} cmmEthConfig_t;

//---------------------------------------------
// Comms Protocol types
//---------------------------------------------
typedef enum
{
  cmmProtocolHDLC,        // 0 = HDLC
  cmmProtocolTransparent, // 1 = No Protocol
  cmmProtocolAsyncEnq,    // 2 = ENQ STX LEN MSG ETX LRC
  cmmProtocolAsyncLength, // 3 = STX LEN MSG ETX LRC 
  cmmProtocolAsyncDle,    // 4 = STX DLE/MSG ETX LRC 
  cmmProtocolAsyncNone,   // 5 = STX MSG(ASCII) ETX LRC 
  cmmProtocolAsyncArgent, // 6 = Argent Terminal Adapter
  cmmProtocolTCP,         // 7 = TCP/IP
  cmmProtocolUDP,         // 8 = UDP
  cmmProtocolAsyncLenCRC, // 9 = STX LEN MSG ETX CRC
  cmmProtocolColesMyer    // 10  = STX LEN MSG ETX CRCprop 
  
 
} cmmProtocol_t;

//---------------------------------------------
// External Device Types
//---------------------------------------------
typedef enum
{
  cmmExtSerial,
  cmmExtEthernet
} cmmExternalType_t;

//---------------------------------------------
// Length Format
//---------------------------------------------
typedef enum
{
  cmmLengthBCD,     	// 0	100 = 0x01 0x00
  cmmLengthHexHiLo,     // 1	100 = 0x00 0x64
  cmmLengthHexLoHi,  	// 2	100 = 0x64 0x00
  cmmLengthNone 		// 3	NULL
} cmmLengthFormat_t;


//---------------------------------------------
// Return codes (int16)
//---------------------------------------------
typedef enum
{
  cmmSuccess,					//
  cmmErrTimeout,				//
  cmmErrLostCarrier,			//
  cmmErrGeneral,				
  cmmErrInitFailed,
  cmmErrRxBufferOverflow,
  cmmErrAsyncNoAck,
  cmmErrAsyncNoResponse,
  cmmErrBadLRC,
  cmmErrNoETX,
  cmmErrNoENQ,					//10
  cmmErrInvalidLength,
  cmmErrNotInitialised,
  cmmErrInitialised,
  cmmErrTxBufferOverflow,
  cmmErrApplicationMissing,		//15
  cmmErrInternalBufferOverflow,
  cmmErrPhoneNumberMissing,
  cmmErrUserCancel,
  cmmErrUserTimeout,
  cmmErrOffBase,				//20
  cmmErrDeviceInUse,
  cmmErrSystem,
  cmmErrDialNoCarrier,			//23
  cmmErrDialNoTone,				//24
  cmmErrDialFailure,			//25
  cmmErrDialBusy,				//26
  cmmErrEthInterfaceDown,
  cmmErrEthNoRemoteIpAddr,
  cmmErrEthHostConnectFail,
  cmmErrEthHostDisconnected,	//30
  cmmErrSignalCtsOff,
  cmmErrNoEOT
} cmmResult_t;
  
//--------------------------
// Comms Commands (uint32)
//--------------------------
typedef enum
{
  CMM_COMMAND_INIT,
  CMM_COMMAND_SEND,
  CMM_COMMAND_RECV,
  CMM_COMMAND_LISTEN,
  CMM_COMMAND_STATUS,
  CMM_COMMAND_END,
  CMM_COMMAND_LAST_ERROR_GET,	// 6. Error/Error Text
  CMM_COMMAND_MODEM_INFO_GET,	// 7. Firmware Version
  CMM_COMMAND_DEBUG_SET,		// 8
  CMM_COMMAND_DEBUG_GET,		// 9
  CMM_COMMAND_DIAL_INFO_SET,	// 10 Blind/Busy Tone/Pulse/PABX/Connection Mode
  CMM_COMMAND_DIAL_INFO_GET,
  CMM_COMMAND_ETH_INFO_SET,   // Address/Mask/Gateway/DHCP
  CMM_COMMAND_ETH_INFO_GET,   //                       + /Presence
  CMM_COMMAND_EDH_INFO_SET,   // Heartbeat/Params/AutoReceive
  CMM_COMMAND_EDH_INFO_GET,		//15
  CMM_COMMAND_RS232_INFO_SET, // Async Ack Wait
  CMM_COMMAND_RS232_INFO_GET,
  CMM_COMMAND_DEVICE_FALLBACK_SET,
  CMM_COMMAND_CONFIGURATION_PRINT,
  CMM_COMMAND_LNET_UP,      // UP LNET
  CMM_COMMAND_LNET_DOWN,    // DOWN LNET
  CMM_COMMAND_PING,
  CMM_COMMAND_DEVICE_INFO_SET,
  CMM_COMMAND_DEVICE_INFO_GET,
  CMM_COMMAND_FILE_SEND  
} cmmCommand_t;



//aan
#define FLUSH_SEND_BUFFER	0x01
#define FLUSH_RECV_BUFFER	0x02
//aan

typedef struct 
{
	uint16		comport;////1-COM1 2-COM2 3-COM3 4-blablabla	
	bits32		dataSize:4;		
	bits32  		parity:4;	
	bits32		stopBits:4;
	bits32		rfu:12;
	uint32 		speed;
	
}cmmRS232InitParam_t;



/*************************************/

typedef struct 
{		
	uint32	speed;
	bits32 	fastconnect:1;
	bits32 	blindmode:1;
	bits32 	pulsedial:1;
		
	bits32 	debug:1;
	bits32 	usercancel:1;
	bits32 	foreground:1;
	bits32	ASCIImode:1;
	bits32 	backup:1;
	

	
	char		pri_num[17];			// Primary Host Number
	char		sec_num[17];			// Secondary Host Number

	
}cmmDialParam_t;


/*************************************/

typedef struct 
{	
	uint8	username[32];
	uint8	password[32];
	bits32	lenFormat:4;
	bits32	rfu:28;
	uint8	ipAddrPri[4];
	int16	portNumPri;
	//uint16	portNumPri;
	uint8	ipAddrSec[4];
	int16	portNumSec;
	//uint16	portNumSec;
	
	
}cmmTcpipInitParam_t;


typedef struct
{
	cmmDialParam_t			Dial;
	cmmRS232InitParam_t		RS232;
	cmmTcpipInitParam_t		IP;		
}Phyical_link;

 typedef struct
{
	uint16 	cmmmode:8;
	uint16 	protocol:8;
	uint16	pri_attempts:8;
	uint16	sec_attempts:8;	
	
	uint16	rx_timeout;	
	uint16	connect_timeout;	 
	uint16	inter_char_time_out;
	Phyical_link cmm;	
}INIT_COMMS_PARAMS;


 
//---------------------------------------------
// Exported Functions
//---------------------------------------------
//cmmResult_t cmmInit(uint32* deviceHandle,INIT_COMMS_PARAMS *icp);
cmmResult_t cmmInit(uint32* deviceHandle,INIT_COMMS_PARAMS *icp, bits32  predial);
cmmResult_t cmmEnd(uint32 deviceHandle);
int16 cmmSend(uint32 deviceHandle, uint8 const* msg, size_t length);
int16 cmmRecv(uint32 deviceHandle, uint8* msg, uint16* length, size_t maxSize);
int16 cmmListen(uint32 deviceHandle);
int16 cmmStatus(uint32 deviceHandle);
int16 cmmGeneral(void);
int16 cmmDebug(char const* text, void const* buffer, size_t bufferLength);
int16 cmmPrintf(char const* format, ...);
int16 cmmTestMsg(const S_TRANSIN *msg);
void InitDefaultConfig(void);
void InitClearHandle(void);

//aan
void cmmFlushComBuffer(uint32 deviceHandle, uint8 buff2Clear);
cmmResult_t cmmGPRS_Stop(void);
cmmResult_t cmmGPRS_Start(uint8 gprsPredial,uint8 check);
cmmResult_t cmmGPRSInit(uint8 gprsPredial,uint8 check);
//aan

//#ifdef __TEST_SEC_DIAL__
//cmmResult_t cmmLastErr(uint32 deviceHandle);
cmmResult_t cmmLastErr(int connRetval);
void cmmSetDial(uint8 DialPri);
uint8 GetcmmSetDial(void);
void killthread(void);
//#endif


//aan, 15 Juli 2007
cmmResult_t cmmGetConnectionStatus(void);

cmmResult_t cmmGetConnectionStatus2(uint32 deviceHandle);

#ifdef _SSL_ENABLE_

cmmResult_t  cmmSslEnd();
int16 cmmSslSend(const uint8 * msg, size_t length);
int16 cmmSslRecv(uint8* msg, uint16* length, size_t maxSize);

#endif

//// Types //////////////////////////////////////////////////////
typedef struct
{
	int m_nLink;
	int m_nBaudrate;
	int m_nBitsPerByte;
	int m_nStopBits;
	int m_nParity;
	int m_nFlowCtrl;

	int m_bUseModem;
	int m_nModemCmdTerminator;
	char m_szInitString[LL_MODEM_L_INIT_STRING_MAX + 1];
	char m_szPhoneNumber[LL_MODEM_L_PHONE_NUMBER_MAX + 1];


	int m_bUseGsm;
	int m_nGsmMode;
	int m_nGsmModulation;
	int m_nConnectionTimeout;

	char m_szPinCode[LL_GPRS_L_PIN_CODE_MAX+1];

	int m_bUseHdlc;

	int m_bUsePpp;
	char m_szLogin[LL_PPP_L_LOGIN_MAX + 1];
	char m_szPassword[LL_PPP_L_PASSWORD_MAX + 1];

	int m_bUseTcpIp;
	char m_szHostName[LL_TCPIP_L_HOST_NAME_MAX + 1];
	int m_nPort;

	int m_bUseGprs;
	char m_szAPN[LL_GPRS_L_APN_MAX + 1];

	int m_nTestMode;
} T__LL_CONFIGURATION;



#define      SELECTED_APN		1
#define      SELECTED_PABX		SELECTED_APN+1
#define      SELECTED_USR		SELECTED_APN+2
#define      SELECTED_PASS		SELECTED_APN+3


typedef struct nvmtemp
{
	char 	APN[32];
	char       pabx[17];
       char 	username[32];
	char		password[32];

}nvmtemp_t;

#define SIZE_TEMPNVM   sizeof (nvmtemp_t)


#define MAXBUFFER_COMMS		700
typedef struct{
	byte comms_id;
	byte comms_cmd;
	uint32 comms_Hdev;
	INIT_COMMS_PARAMS ipc;	
	bits32 predial;

	size_t lensend;
	uint16  lenrecv;
	size_t lenMAX;
	//SSL handle
	SSL_PROFILE_HANDLE sslProfileHandle;
	SSL_HANDLE sslHandle;
	byte SSL;
	byte TMS;
	byte buffsend[MAXBUFFER_COMMS];
	byte buffrecv[MAXBUFFER_COMMS];
	byte retval;
	
	int16 status;
	byte rfu;
}ST_Comms;


#define SERVICE_COMMS_INIT 					0
#define SERVICE_COMMS_CONNECT					1
#define SERVICE_COMMS_STATUS					2
#define SERVICE_COMMS_DISCONNECT				3
#define SERVICE_COMMS_RECONNECT				4
#define SERVICE_COMMS_SEND					5
#define SERVICE_COMMS_RECEIVE					6
#define SERVICE_COMMS_LISTEN					7
#define SERVICE_COMMS_FLUSH_BUFFER			8
#define SERVICE_COMMS_INIT_DEFAULT			9
#define SERVICE_COMMS_SET_DIAL_PRI			10
#define SERVICE_COMMS_CLEAR_HANDLE			11
#define SERVICE_COMMS_KILL_THREAD				12
#define SERVICE_COMMS_GET_CONNECTSTATUS2	13
#define SERVICE_COMMS_SSL_SEND				14
#define SERVICE_COMMS_SSL_RECV				15
#define SERVICE_COMMS_SSL_END					16
#define SERVICE_COMMS_GPRS_STOP				17
#define SERVICE_COMMS_GPRS_INIT				18
#define SERVICE_COMMS_END						19
#define SERVICE_COMMS_LAST_ERR				20
#define SERVICE_COMMS_SSL_SET					21
#define SERVICE_COMMS_GEN_SET_PABX					22
#define SERVICE_COMMS_GEN_SET_APN					23
#define SERVICE_COMMS_GEN_SET_USERNAME					24
#define SERVICE_COMMS_GEN_SET_PASSWORD					25
#define SERVICE_COMMS_SSL_PRINT_PROFILE					26
#define SERVICE_COMMS_GPRS_GET_NET_STATUS				27


extern int Service_COMMS(int nSize, ST_Comms *hcomms);
//extern int fm_ServiceCOMGet(unsigned short appli_id, unsigned short serv_id,unsigned char *priority);

//extern void fm_InitDefaultConfig(void);
//extern int fm_connect(uint32 deviceHandle,INIT_COMMS_PARAMS icp);
//extern int16 fm_Send(uint32 deviceHandle, uint8 const* msg, size_t length);
//extern int16 fm_Recv(uint32 deviceHandle, uint8 * msg, uint16* length,size_t maxSize);
//extern uint16 fm_Listen(uint32 deviceHandle);
//extern int16 fm_Status(uint32 deviceHandle);
//extern void fm_FlushComBuffer(uint32 deviceHandle, uint8 buff2Clear);
//extern int fM_RecBufModem(uint32 deviceHandle, uint8* msg, uint16* len, size_t maxSize);
//extern void fm_cmmSetDial(uint8 DialPri);
//extern cmmResult_t fm_cmmInit(uint32* deviceHandle,INIT_COMMS_PARAMS *icp, bits32 predial);
//extern void fm_InitClearHandle	(void);
//extern void fm_killthread(void);
//extern cmmResult_t fm_cmmGetConnectionStatus2(uint32 deviceHandle);
//extern int16 fm_cmmSslSend(const uint8 * msg, size_t length);
//extern int16 fm_cmmSslRecv(uint8* msg, uint16* length, size_t maxSize);
//extern cmmResult_t  fm_cmmSslEnd(void);
//extern cmmResult_t fm_cmmGPRS_Stop(void);
//extern cmmResult_t fm_cmmGPRSInit(uint8 gprsPredial, uint8 check);
//extern void fm_tmsInitCommsInfo(void);
//extern uint8 fm_tmsConnectToHost(void);
//extern cmmResult_t  fm_cmmEnd(uint32 deviceHandle);
//extern uint8 GenSetFile(uint8 selfile);
//extern int8 fm_SslIsConnecting(void );
//extern int16 fm_SslGetLastError(void);
//extern int8 	fm_SslSetProfile(void);
//extern void fm_PrintSSLProfile(void);
extern char * nvmGetPabx(void);
extern char* nvmAPNGet(void);
extern char* nvmUserNameGet(void);
extern char* nvmPasswordGet(void);
extern char * ReadSelectedFile(uint8 selected);

//04012011
//extern int fm_CheckGPRSNetStatus(int* GPRS_Stat);

#endif
